package com.xingdata.jjxc.activity.viewdata;

import jar.MessageManager;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.RequestCode;
import com.xingdata.jjxc.activity.CorrectSpeedActivity;
import com.xingdata.jjxc.activity.FMSetting;
import com.xingdata.jjxc.activity.Feedbackavt;
import com.xingdata.jjxc.activity.HUDUpgradeActivity;
import com.xingdata.jjxc.activity.HomeCompanySetActivity;
import com.xingdata.jjxc.activity.HudUpdateActivity;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.activity.SettingsActivity;
import com.xingdata.jjxc.activity.UserInfoActivity;
import com.xingdata.jjxc.favorites.FavoritesActivity;
import com.xingdata.jjxc.m.avt.Aboutavt;
import com.xingdata.jjxc.m.avt.Commonaddavt;
import com.xingdata.jjxc.m.avt.HUDSummavt;
import com.xingdata.jjxc.map.navi.activity.OfflineMapActivity;
import com.xingdata.jjxc.utils.ImageUtils;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.CircleImageView;
import com.xingdata.jjxc.views.Widget;

/**
 * “我的”页面
 * 在MainActivity中new MyViewData（）的时候，构造函数进行初始化（界面+功能）
 */
public class MyViewData implements OnClickListener {
	private View tabView;
	private Context mContext;
	private CircleImageView my_head_iv;
	private TextView my_name_tv,Offline_Map_tv,settingnew_tv,my_common_address_tv,
		my_hud_summary_tv,my_about_tv,my_feed_back_tv,HUD_Upgrade_tv,FM_Setting_tv;
	private int flag;
	private Bundle mBundle;
	private Dialog dialog_login;

	public MyViewData(View tabView, Context mContext, Handler mHandler) {
		this.tabView = tabView;
		this.mContext = mContext;
		initView();
	}

	private void initView() {
		Typeface font = Typeface.createFromAsset(this.mContext.getAssets(), "fonts/PingFang-SC-Regular.ttf");
		my_head_iv = (CircleImageView) tabView.findViewById(R.id.my_head_iv);
		my_head_iv.setOnClickListener(this);
		
		my_name_tv = (TextView) tabView.findViewById(R.id.my_name_tv);
		Offline_Map_tv = (TextView) tabView.findViewById(R.id.Offline_Map_tv);
		settingnew_tv = (TextView) tabView.findViewById(R.id.settingnew_tv);
		my_common_address_tv = (TextView) tabView.findViewById(R.id.my_common_address_tv);
		my_hud_summary_tv = (TextView) tabView.findViewById(R.id.my_hud_summary_tv);
		my_about_tv = (TextView) tabView.findViewById(R.id.my_about_tv);
		my_feed_back_tv = (TextView) tabView.findViewById(R.id.my_feed_back_tv);
		HUD_Upgrade_tv = (TextView) tabView.findViewById(R.id.HUD_Upgrade_tv);
		((TextView) tabView.findViewById(R.id.clear_cache)).setTypeface(font);
		((TextView) tabView.findViewById(R.id.correct_speed)).setTypeface(font);
		((TextView) tabView.findViewById(R.id.FM_Setting_tv)).setTypeface(font);
		
		my_name_tv.setTypeface(font);
		Offline_Map_tv.setTypeface(font);
		settingnew_tv.setTypeface(font);
		my_common_address_tv.setTypeface(font);
		my_hud_summary_tv.setTypeface(font);
		my_about_tv.setTypeface(font);
		my_feed_back_tv.setTypeface(font);
		HUD_Upgrade_tv.setTypeface(font);

		// my_goback_fl = (FrameLayout) tabView.findViewById(R.id.my_goback_fl);
		((LinearLayout) tabView.findViewById(R.id.settingnew)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.my_common_address_rl)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.my_hud_summary_rl)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.my_feed_back_rl)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.my_about_rl)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.Offline_Map_RelativeLayout)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.HUD_Upgrade_RelativeLayout)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.FM_Setting_RelativeLayout)).setOnClickListener(this);
//ysc20160809新UI中去除了上传HUD运行日志		((LinearLayout) tabView.findViewById(R.id.update_log_RelativeLayout)).setOnClickListener(this);

		((LinearLayout) tabView.findViewById(R.id.clear_cache_RelativeLayout)).setOnClickListener(this);
		((LinearLayout) tabView.findViewById(R.id.correct_speed_RelativeLayout)).setOnClickListener(this);//矫正车速

		if (SystemInfo.getUserInfo() != null) {
			my_name_tv.setText(SystemInfo.getUserInfo().getUsername());
			initHead();
		}

	}

	/** 主界面“我的”头像初始化设置 */
	public void initHead() {
		// TODO Auto-generated method stub
		if (SystemInfo.getUserInfo().getHead() != null) {
			if (SystemInfo.getUserInfo().getHead().contains("http://")) {
				App.imageLoader.displayImage(SystemInfo.getUserInfo().getHead(), my_head_iv, App.options);
			} else {
				App.imageLoader.displayImage(App.SEGMENTSOURCE + SystemInfo.getUserInfo().getHead(), my_head_iv,
						App.options);
			}
		}
		if (SystemInfo.getUserInfo().getUsername() != null) {
			my_name_tv.setText(SystemInfo.getUserInfo().getUsername());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.settingnew:
			Intent intent = new Intent(mContext, SettingsActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.my_head_iv:
			flag = 0;
			Widget.withDrawKeyboard((Activity) mContext);
			if (SystemInfo.isLogin()) {
				// showPicDialog();
				((Activity) mContext).startActivityForResult(new Intent(mContext, UserInfoActivity.class),
						RequestCode.REQUESTCODE_UPDATA_ICON);
			} else {
				showLoginDialog();
			}

			break;
		// case R.id.my_goback_fl:// 返回
		// finish();
		// // startActivity(new Intent(Perinfoavt.this, Mainavt.class));
		// break;

		case R.id.my_common_address_rl:// 常用地址
			if (isLogin()) {
				Intent intent_collect_site =new Intent(mContext, FavoritesActivity.class);
				mContext.startActivity(intent_collect_site);
				
//				intent = new Intent(mContext, Commonaddavt.class);
//				intent.putExtra("intent_code", LoginActivity.INTENT_CODE_COMMON_ADDRESS);
				// mBundle = ((Activity) mContext).getIntent().getExtras();
				// intent.putExtras(mBundle);

//				((Activity) mContext).startActivityForResult(intent, RequestCode.RequestCode_BlueListView);

			}

			break;
		// // case R.id.my_car_infomation_rl:// 车辆信息
		// // startActivity(new Intent(Perinfoavt.this, CarInfomatavt.class));
		// // break;
			// 简介
		case R.id.my_hud_summary_rl:
			Intent intent1 = new Intent(mContext, HUDSummavt.class);
			mBundle = new Bundle();
			mBundle.putString("url", App.ABOUT_APP_URL);
			intent1.putExtras(mBundle);
			mContext.startActivity(intent1);
			break;
			// 意见反馈
		case R.id.my_feed_back_rl:
			if (isLogin()) {
				intent = new Intent(mContext, Feedbackavt.class);
				intent.putExtra("intent_code", LoginActivity.INTENT_CODE_COMMON_FEED);
				mContext.startActivity(intent);
			}
			break;
			// 关于
		case R.id.my_about_rl:
			mContext.startActivity(new Intent(mContext, Aboutavt.class));
			break;

		case R.id.Offline_Map_RelativeLayout:
			mContext.startActivity(new Intent(mContext, OfflineMapActivity.class));
			break;
			// HUD升级
		case R.id.HUD_Upgrade_RelativeLayout:
			mContext.startActivity(new Intent(mContext, HudUpdateActivity.class));
			break;
			// FM设置
		case R.id.FM_Setting_RelativeLayout:
			mContext.startActivity(new Intent(mContext, FMSetting.class));
			break;
		// 清理缓存
		case R.id.clear_cache_RelativeLayout:
			clearCache();
			break;
		case R.id.correct_speed_RelativeLayout:
			mContext.startActivity(new Intent(mContext, CorrectSpeedActivity.class));
			break;

		// 上传日志
//ysc20160809新UI中去除了上传HUD运行日志			
//		case R.id.update_log_RelativeLayout:
//			JSONObject jsonObject = new JSONObject();
//			jsonObject.put("username", SystemInfo.getUserInfo().getUsername());
//			jsonObject.put("userid", SystemInfo.getUserInfo().getUserid());
//			jsonObject.put("mobile", SystemInfo.getUserInfo().getMobile());
//			MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), new RequestEntity(
//					jsonObject.toJSONString(), InstructionSet.INSTRUCTION_AUTH_UPLOAD_LOG + ""));
//			break;
		default:
			break;
		}
	}

	private boolean isLogin() {
		if (!SystemInfo.isLogin()) {
			((Activity) mContext).startActivityForResult(new Intent(mContext, LoginActivity.class),
					RequestCode.REQUESTCODE_COMMON_LOGIN);
			return false;
		} else {
			return true;
		}
	}

	private void showLoginDialog() {
		if (dialog_login == null) {
			dialog_login = new Dialog(mContext, R.style.popup_fade_anim);

			// 设置显示动画
			Window window = dialog_login.getWindow();
			window.setWindowAnimations(R.style.popup_fade_anim);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
			window.setAttributes(wl);
			dialog_login.onWindowAttributesChanged(wl);
			dialog_login.setCanceledOnTouchOutside(true);

			View v = LayoutInflater.from(mContext).inflate(R.layout.popup_login, null);
			View blank = v.findViewById(R.id.blank_view);

			blank.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog_login.dismiss();
				}
			});
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			dialog_login.addContentView(v, params);

			Button login = (Button) v.findViewById(R.id.login);
			Button choose_from_piclib = (Button) v.findViewById(R.id.choose_from_piclib);
			Button cancel = (Button) v.findViewById(R.id.cancel);
			login.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					isLogin();
					dialog_login.dismiss();
				}
			});
			cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog_login.dismiss();
				}
			});
		}
		if (!((Activity) mContext).isFinishing() && dialog_login != null && !dialog_login.isShowing()) {
			dialog_login.show();
		}
	}

	public void onActivityResult() {
		initHead();

	}

	/**
	 * 清除缓存
	 */
	private void clearCache() {
		final Dialog exitDialog = new Dialog(mContext, R.style.my_dialog);
		View v = LayoutInflater.from(mContext).inflate(R.layout.pop_register, null);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		exitDialog.addContentView(v, params);
		View blank = v.findViewById(R.id.register_view);
		((TextView) v.findViewById(R.id.exit_TextView)).setText("是否清除所有缓存");
		Button exit = (Button) v.findViewById(R.id.register_exit);
		Button cancel = (Button) v.findViewById(R.id.register_no);

		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				exitDialog.dismiss();
			}
		});
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				exitDialog.dismiss();
				if (SDCardTools.clearCache()) {
					Toast.makeText(mContext, "清除缓存成功", Toast.LENGTH_SHORT).show();
				}

			}
		});

		blank.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				exitDialog.dismiss();
			}
		});
		if (!exitDialog.isShowing()) {
			exitDialog.show();
		}

	}

	public void onDestroy() {
		ImageUtils.releaseImageViewResouce(my_head_iv);
	}
}
